package com.meizu.asum.richtextsdk.tools.selectpictools.selecter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import com.meizu.asum.richtextsdk.tools.selectpictools.vo.MediaVO;

import java.io.File;
import java.util.Date;

public class CropPicture {
    private static Uri cropUri;

    public static void cropImage(Activity activity, MediaVO mediaVO) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(mediaVO.getUri(), "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);

        String filePath = "";
        if (mediaVO.getCachePath() != null) {
            filePath = mediaVO.getCachePath();
        } else {
            filePath = activity.getExternalFilesDir(Environment.DIRECTORY_DCIM).getPath() + "/XMediaImageSelector/Cache/";
        }
        new File(filePath).mkdirs();
        File file = new File(filePath + new Date().getTime() + ".jpg");

        cropUri = Uri.fromFile(file);
        intent.putExtra("output", cropUri);

        intent.putExtra("aspectX", (int) mediaVO.getScaleW());
        intent.putExtra("aspectY", (int) mediaVO.getScaleH());
        intent.putExtra("outputFormat", "JPG");
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", false);

        activity.startActivityForResult(intent, XPictureSelector.REQUEST_CODE_CROP_IMAGE);
    }

    public static Uri getUri() {
        return cropUri;
    }
}
